package com.fitso.model.dao;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.fitso.model.bean.measure.mass.Ounce;
import com.fitso.model.bean.nutrition.Serving;

public abstract class ServingDaoDB_IT extends DatabaseIntegrationTestUnit {
	protected ServingDao servingDao;
	protected List<Serving> servings;

	public abstract ServingDao newServingDao();

	@Before
	public void setUp() throws Exception {
		super.setUp();

		servingDao = newServingDao();
	}

	@Test
	public void test_round_tripping_mass() {
		Serving serving = new Serving(new Ounce(6.2), "apple 2");
		serving.addSubServing(new Serving(new Ounce(6.2).multiply(10), "10 apples"));
		serving.addSubServing(new Serving(new Ounce(6.2).multiply(100), "100 apples"));
		serving.addSubServing(new Serving(new Ounce(6.2).multiply(1000), "1000 apples"));

		servingDao.createOrUpdate(serving);

		Long id = serving.getId();

		Serving actual = servingDao.readById(id);
		assertNotNull(actual);
		assertThat(serving, equalTo(actual));
		assertThat(serving.getMeasure(), equalTo(actual.getMeasure()));

		serving.setDescription("updated description");
		servingDao.createOrUpdate(serving);

		actual = servingDao.readById(id);
		assertThat(serving, equalTo(actual));
		assertThat(serving.getDescription(), equalTo(actual.getDescription()));

		servingDao.delete(serving);
		assertNull(servingDao.readById(id));
	}
}
